package com.yeung.controller;

import com.yeung.beans.Greeting;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

/**
 * controller处理类
 *
 * @ResponseBody 标注在类上， 则所有方法都输出Json格式
 * @RestController 相当于 @Controller +@ResponseBody，是ajax异步请求。无法返回jsp页面的
 */

@Controller
public class GreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    @ResponseBody
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }

    @RequestMapping("/show")
    public String show(ModelMap map) {
        //映射的是classpath:/templates/success.html
        map.put("leaf", "这句话来自服务器, 需要thymeleanf来解析");
        map.put("names", Arrays.asList("yeung", "alex", "john"));
        map.put("uText", "<h1 >待转义的内容</h1>");
        return "success";
    }

    @RequestMapping({"/","/index"})
    public String index() {
        return "index";
    }
}
